﻿using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Interface.SystemSetting;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Service.DataMaintenance;
using PhysicalExamination5.WebApi.Utility.JWT;
using System.IdentityModel.Tokens.Jwt;

namespace PhysicalExamination5.WebApi.Controllers.DataMaintenance
{
    /// <summary>
    /// 字典维护
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class ZiDianWeiHuController : Controller
    {
        private readonly IZiDianWeiHu _IZiDianWeiHu;
        private readonly ICustomJWTService _ICustomJWTService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private static readonly ILog log = LogManager.GetLogger(typeof(IndexController));

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="IZiDianWeiHu"></param>
        /// <param name="ICustomJWTService"></param>
        /// <param name="httpContextAccessor"></param>
        public ZiDianWeiHuController(IZiDianWeiHu IZiDianWeiHu, ICustomJWTService ICustomJWTService, IHttpContextAccessor httpContextAccessor)
        {
            _IZiDianWeiHu = IZiDianWeiHu;
            _ICustomJWTService = ICustomJWTService;
            _httpContextAccessor = httpContextAccessor;

            //配置日志ip地址参数信息
            string ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            log4net.GlobalContext.Properties["localIpAddress"] = ipAddress;
        }

        /// <summary>
        /// 查询字典
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelZiDian()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZiDianWeiHu.GetSelZiDian());
        }
        /// <summary>
        /// 查询字典明细
        /// </summary>
        /// <param name="strYiJiName"></param>
        /// <param name="strErJiName"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSqlZiDianMingXi(string strYiJiName, string strErJiName)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZiDianWeiHu.GetSqlZiDianMingXi(strYiJiName, strErJiName));
        }
        /// <summary>
        /// 字典明细新增
        /// </summary>
        /// <param name="ziDianMingXiAdd"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostZiDianMingXiAdd(ZiDianMingXiAddClass ziDianMingXiAdd)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IZiDianWeiHu.PostZiDianMingXiAdd(ziDianMingXiAdd, strYuanQuQianZhui));
        }
        /// <summary>
        /// 字典明细修改
        /// </summary>
        /// <param name="ziDianMingXiUpdate"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutZiDianMingXiUpdate(ZiDianMingXiUpdateClass ziDianMingXiUpdate)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZiDianWeiHu.PutZiDianMingXiUpdate(ziDianMingXiUpdate));
        }
        /// <summary>
        /// 字典明细删除
        /// </summary>
        /// <param name="ziDianMingXiDelete"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteZiDianMingXiDelete(ZiDianMingXiDeleteClass ziDianMingXiDelete)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZiDianWeiHu.DeleteZiDianMingXiDelete(ziDianMingXiDelete));
        }
    }
}
